/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import job.CalculateTimeJob;
import job.CalculateTimeJobListener;
import job.CalculateTimeTriggerListener;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.calendar.AnnualCalendar;
import org.quartz.impl.calendar.CronCalendar;
import quartz.JobBuilder;
import quartz.SchedulerManager;
import quartz.TriggerBuilder;

/**
 *
 * @author yjbian
 */
public class SimpleExample {

    public static void testJobNoDataMap() throws SchedulerException {
        JobDetail jobDetail = JobBuilder.buildJobDetail("SimpleJob001", "SimpleGroup", new CalculateTimeJob());

        long interval = 5000L;
        Trigger trigger = TriggerBuilder.buildSimpleTrigger("Simpletrigger001", "SimpleGroup", new Date(), interval);

        SchedulerManager.scheduleJob(jobDetail, trigger);
    }

    public static void testJobWithDataMap() throws SchedulerException {
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("BaseTime", "2015-01-01 00:00:00");
        JobDetail jobDetail = JobBuilder.buildJobDetail("SimpleJob002", "SimpleGroup", new CalculateTimeJob(), jobDataMap);

        Date startTime = new Date();
        long endTimeValue = startTime.getTime() + 30 * 1000L;
        Date endTime = new Date(endTimeValue);

        long interval = 3000L;
        Trigger trigger = TriggerBuilder.buildSimpleTrigger("Simpletrigger002", "SimpleGroup", startTime, endTime, interval);

        SchedulerManager.scheduleJob(jobDetail, trigger);
    }

    public static void testJobWithCalendar() throws SchedulerException {
        JobDetail jobDetail = JobBuilder.buildJobDetail("SimpleJob001", "SimpleGroup", new CalculateTimeJob());

        // exclude day
        AnnualCalendar annualCalendar = new AnnualCalendar();
        Calendar today = Calendar.getInstance();
        today.set(Calendar.MONTH, 6);
        today.set(Calendar.DATE, 6);             
        annualCalendar.setDayExcluded(today, true);
        SchedulerManager.getScheduler().addCalendar("today", annualCalendar, true, true);

        long interval = 5000L;
        
        Trigger trigger = TriggerBuilder.buildSimpleTrigger("Simpletrigger001", "SimpleGroup", new Date(), interval);
        trigger.setCalendarName("today");

        SchedulerManager.scheduleJob(jobDetail, trigger);
    }
    
    
    public static void testJobListener() throws SchedulerException {
        JobDetail jobDetail = JobBuilder.buildJobDetail("SimpleJob001", "SimpleGroup", new CalculateTimeJob());

        long interval = 5000L;
        Trigger trigger = TriggerBuilder.buildSimpleTrigger("Simpletrigger001", "SimpleGroup", new Date(), interval);

        SchedulerManager.getScheduler().addJobListener(new CalculateTimeJobListener());
        jobDetail.addJobListener("CalculateTimeJobListener");
        
        SchedulerManager.scheduleJob(jobDetail, trigger);
    }
    
    public static void testTriggerListener() throws SchedulerException {
        JobDetail jobDetail = JobBuilder.buildJobDetail("SimpleJob001", "SimpleGroup", new CalculateTimeJob());

        long interval = 5000L;
        Trigger trigger = TriggerBuilder.buildSimpleTrigger("Simpletrigger001", "SimpleGroup", new Date(), interval);

        SchedulerManager.getScheduler().addTriggerListener(new CalculateTimeTriggerListener());     
        trigger.addTriggerListener("CalculateTimeTriggerListener");
        
        SchedulerManager.scheduleJob(jobDetail, trigger);
    }
}
